import { inject, type App } from "vue";
import { useSimpleToast as useToast } from "./simple.toast";
import type { ToastOptions } from "./simple.toast";

export interface SimpleToastPlugin {
  /**
   * 显示一个普通的toast消息
   * @param message 要显示的消息
   * @param options 可选的配置项
   */
  show(message: string, options?: Partial<ToastOptions>): void;

  /**
   * 显示一个成功的toast消息
   * @param message 要显示的消息
   * @param options 可选的配置项
   */
  success(message: string, options?: Omit<Partial<ToastOptions>, "type">): void;

  /**
   * 显示一个错误的toast消息
   * @param message 要显示的消息
   * @param options 可选的配置项
   */
  error(message: string, options?: Omit<Partial<ToastOptions>, "type">): void;

  /**
   * 显示一个信息toast消息
   * @param message 要显示的消息
   * @param options 可选的配置项
   */
  info(message: string, options?: Omit<Partial<ToastOptions>, "type">): void;

  /**
   * 显示一个警告toast消息
   * @param message 要显示的消息
   * @param options 可选的配置项
   */
  warning(message: string, options?: Omit<Partial<ToastOptions>, "type">): void;
}

// 为 Vue 扩展全局属性
declare module "vue" {
  export interface ComponentCustomProperties {
    $simpleToast: SimpleToastPlugin;
  }
}

const SIMPLE_TOAST_SYMBOL = Symbol("simple-toast");
const simpleToast = useToast();
export const plugin = {
  install(app: App) {
    app.config.globalProperties.$simpleToast = simpleToast;
    app.provide(SIMPLE_TOAST_SYMBOL, simpleToast);
  },
};

export function useSimpleToast(): SimpleToastPlugin {
  return inject<SimpleToastPlugin>(SIMPLE_TOAST_SYMBOL) || simpleToast;
}

export default plugin;
